//
//  SunOrderCell.m
//  Xspcf
//
//  Created by sunshine on 16/10/27.
//  Copyright © 2016年 小小算盘. All rights reserved.
//

#import "SunOrderCell.h"
#import "SunTriangle.h"

@interface SunOrderCell()
{
    UILabel *_time;
    UILabel *_progress;
    UILabel *_title;
    UILabel *_key1;
    UILabel *_key2;
    UILabel *_key3;
    UILabel *_value1;
    UILabel *_value2;
    UILabel *_value3;
    UILabel *_number;
}
@end
@implementation SunOrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"cellID";
    SunOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[SunOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell creatSubviews];
    }
    return cell;
}

- (void)creatSubviews{
    UIImageView *bg = [[UIImageView alloc]init];
    [self addSubview:bg];
    [bg lt_All:SunEdgesMake(0, 0, 0, 0) save:YES];
    bg.image = [Sun imageWithColor:BACKGROUND];
    UIImageView *line1 = [[UIImageView alloc]init];
    [bg addSubview:line1];
    [line1 lt_Left:SunEdgesMake(0, X(28), 0, 1) to:nil save:YES];
    line1.image = [Sun imageWithColor:GRAY];
    UIImageView *clock = [[UIImageView alloc]init];
    [bg addSubview:clock];
    [clock lt_XcenterTo:line1 save:YES];
    [clock lt_setTop:Y(8) to:nil save:YES];
    clock.image = [UIImage imageNamed:@"third_order_clock"];
    _time = [[UILabel alloc]init];
    [bg addSubview:_time];
    [_time lt_YcenterTo:clock save:YES];
    [_time lt_setLeft:X(7) to:clock save:YES];
    _time.textColor = BLACK;
    _time.font = [UIFont systemFontOfSize:13];
    _progress = [[UILabel alloc]init];
    [bg addSubview:_progress];
    [_progress lt_YcenterTo:_time save:YES];
    [_progress lt_setRight:X(27) to:nil save:YES];
    _progress.font = [UIFont systemFontOfSize:10];
    SunTriangle *tri = [[SunTriangle alloc]initWithFrame:CGRectMake(0, 0, X(26), Y(52))];
    [bg addSubview:tri];
    [tri lt_setLeft:X(5) to:line1 save:YES];
    [tri lt_setSize:CGSizeMake( X(15), Y(30)) save:YES];
    [tri lt_setTop:Y(48) to:nil save:YES];
    tri.backgroundColor = WHITE;
    UIView *content = [[UIView alloc]init];
    [bg addSubview:content];
    [content lt_Left:SunEdgesMake(Y(34), 0, Y(20), X(300)) to:tri save:YES];
    content.backgroundColor = WHITE;
    content.clipsToBounds = YES;
    content.layer.cornerRadius = 4;
    _title = [[UILabel alloc]init];
    [content addSubview:_title];
    [_title lt_setHeight:Y(15) save:YES];
    [_title lt_setTop:Y(13) to:nil save:YES];
    [_title lt_setLeft:Y(20) to:nil save:YES];
    _title.textColor = GRAY;
    _title.font = [UIFont systemFontOfSize:12];
    UIImageView *line2 = [[UIImageView alloc]init];
    [content addSubview:line2];
    [line2 lt_Top:SunEdgesMake(Y(8), X(20), X(20), 0.5) to:_title save:YES];
    line2.image = [Sun imageWithColor:LINE];
    _key1 = [[UILabel alloc]init];
    [content addSubview:_key1];
    [_key1 lt_setTop:Y(15) to:line2 save:YES];
    [_key1 lt_leftEqual:line2 save:YES];
    [_key1 lt_setHeight:Y(13) save:YES];
    _key1.textColor = BLACK;
    _key1.font = [UIFont systemFontOfSize:12];
    _key2 = [Sun copyWithView:_key1];
    [content addSubview:_key2];
    [_key2 lt_YcenterTo:_key1 save:YES];
    [_key2 lt_setLeft:X(30) to:_key1 save:YES];
    _key3 = [Sun copyWithView:_key2];
    [content addSubview:_key3];
    [_key3 lt_YcenterTo:_key2 save:YES];
    [_key3 lt_setLeft:X(30) to:_key2 save:YES];
    _value1 = [[UILabel alloc]init];
    [content addSubview:_value1];
    _value1.textColor = RED;
    _value1.adjustsFontSizeToFitWidth = YES;
    _value1.font = [UIFont systemFontOfSize:15];
    [_value1 lt_setTop:Y(10) to:_key1 save:YES];
    [_value1 lt_leftEqual:_key1 save:YES];
    _value2 = [Sun copyWithView:_value1];
    [content addSubview:_value2];
    [_value2 lt_YcenterTo:_value1 save:YES];
    [_value2 lt_leftEqual:_key2 save:YES];
    _value3 = [Sun copyWithView:_value1];
    [content addSubview:_value3];
    [_value3 lt_YcenterTo:_value1 save:YES];
    [_value3 lt_leftEqual:_key3 save:YES];
    [_value1 sizeToFit];
    [_value2 sizeToFit];
    [_value3 sizeToFit];
    _number = [[UILabel alloc]init];
    [self addSubview:_number];
    [_number lt_YcenterTo:_title save:YES];
    [_number lt_setRight:X(32) to:nil save:YES];
    _number.textColor = GRAY;
    _number.font = FONT(11);
}

- (void)setCellData:(NSDictionary *)data selected:(int)selected{
    _title.text = [data objectForKey:@"name"];
    _time.text = [data objectForKey:@"time"];
    _number.hidden = YES;
    if ([[data objectForKey:@"period"] length] > 0) {
        _number.hidden = NO;
        _number.text = [NSString stringWithFormat:@"期数:%@",[data objectForKey:@"period"]];
    }
    switch (selected) {
        case 1:{
            [_progress setMarkup:[NSString stringWithFormat:@"<font color=\"#333333\">项目进度 </font><font color=\"#ff5971\">%@</font>",[data objectForKey:@"progress"]]];
            _key1.text = @"年化收益率";
//            NSString *apr = [data objectForKey:@"apr"];
//            if ([[data objectForKey:@"addApr"] intValue] > 0) {
//                apr = [apr stringByAppendingString:[data objectForKey:@"addApr"]];
//            }
//            _value1.text = apr;
            _value1.text = [data objectForKey:@"apr"];
            _key2.text = @"项目期限";
            _value2.text = [data objectForKey:@"days"];
            _key3.text = @"投资金额";
//            _value3.text = [data objectForKey:@"account"];
            _value3.text = [NSString stringWithFormat:@"%ld元",[[data objectForKey:@"account"] integerValue]];
            break;
        }
        case 2:{
            [_progress setMarkup:[NSString stringWithFormat:@"<font color=\"#333333\">项目期限 </font><font color=\"#ff5971\">%@</font>",[data objectForKey:@"days"]]];
            _key1.text = @"应收本金";
            _value1.text = [data objectForKey:@"account"];
            _key2.text = @"应收利息";
            _value2.text = [data objectForKey:@"interest"];
            _key3.text = @"还款日";
            _value3.text = [data objectForKey:@"repayTime"];
            break;
        }
        case 3:{
            [_progress setMarkup:[NSString stringWithFormat:@"<font color=\"#333333\">项目期限 </font><font color=\"#ff5971\">%@</font>",[data objectForKey:@"days"]]];
            _key1.text = @"已收本金";
            _value1.text = [data objectForKey:@"account"];
            _key2.text = @"已收利息";
            _value2.text = [data objectForKey:@"interest"];
            _key3.text = @"年化收益率";
//            NSString *apr = [data objectForKey:@"apr"];
//            if ([[data objectForKey:@"addApr"] intValue] > 0) {
//                apr = [apr stringByAppendingString:[data objectForKey:@"addApr"]];
//                apr = [apr stringByAppendingString:@"(加息卡)"];
//            }
            _value3.text = [data objectForKey:@"apr"];
            break;
        }
        default:
            break;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
